//导入模块

const http =require('http');
// const url =require('url');
const path =require('path');
const fs =require('fs');
const os =require('os')
let filePath =path.resolve('./data/history.txt')
// let a=path.resolve('./public');//设置根目录
//创建服务
let server = http.createServer();
server.on('request',(req,res)=>{
    // console.log(req.socket.remoteAddress)
    let a=req.url//客户端请求的地址
    // console.log(req.headers['user-agent'])//客户端的浏览器的信息
    if(path.extname(a) === '.ico'){
        let usermsg=req.headers['user-agent']
        let userIp =req.socket.remoteAddress
        let data = new Date()
        fs.writeFileSync(filePath,userIp+data+usermsg+os.EOL,{flag:'a'})
    }
    let b= fs.readFileSync(filePath)
    res.setHeader('content-type','text/html;charset=utf8');
    res.end(b);
//   let c=  fs.writeFile(filePath)
})
//监听服务端口
server.listen(8080,()=>{
    console.log('开启服务：http://localhost:8080');
})